package com.gxa.servlet;

import com.alibaba.fastjson.JSON;
import com.gxa.dto.CommunityDto;
import com.gxa.entity.extend.QueryAllCommunity;
import com.gxa.service.CommunityService;
import com.gxa.service.impl.CommunityServiceImpl;
import com.gxa.utils.TableResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/page/community/list")
public class CommunityListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        //接收带条件的参数
        String searchParams = req.getParameter("searchParams");
        CommunityDto communityDto = JSON.parseObject(searchParams, CommunityDto.class);
        System.out.println(communityDto);


        CommunityService communityService = new CommunityServiceImpl();
        Integer count = null;
        List<QueryAllCommunity> commList = null;
        try {
            count = communityService.count(communityDto);
            commList = communityService.queryCommunity(Integer.parseInt(page), Integer.parseInt(limit), communityDto);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        TableResult result = new TableResult(0,"success",count,commList);

        String commJsonStr = JSON.toJSONString(result);
        out.print(commJsonStr);


    }
}
